package com.crm.web.controller.business;

import java.util.List;

import com.crm.common.utils.ShiroUtils;
import com.crm.system.domain.BieDoctor;
import com.crm.system.domain.BiePatient;
import com.crm.system.service.IBieDoctorService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.crm.common.annotation.Log;
import com.crm.common.enums.BusinessType;
import com.crm.system.domain.BieAdvice;
import com.crm.system.service.IBieAdviceService;
import com.crm.common.core.controller.BaseController;
import com.crm.common.core.domain.AjaxResult;
import com.crm.common.utils.poi.ExcelUtil;
import com.crm.common.core.page.TableDataInfo;

/**
 * 医嘱Controller
 * 
 * @author lifei
 * @date 2021-05-10
 */
@Controller

@RequestMapping("/system/advice")
public class BieAdviceController extends BaseController
{
    private String prefix = "system/advice";

    @Autowired
    private IBieAdviceService bieAdviceService;

    @Autowired
    private IBieDoctorService bieDoctorService;

    @RequiresPermissions("system:advice:view")
    @GetMapping()
    public String advice()
    {
        return prefix + "/advice";
    }

    /**
     * 查询医嘱列表
     */
    @RequiresPermissions("system:advice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BieAdvice bieAdvice)
    {
        startPage();
        List<BieAdvice> list = bieAdviceService.selectBieAdviceList(bieAdvice);
        return getDataTable(list);
    }

    /**
     * 导出医嘱列表
     */
    @RequiresPermissions("system:advice:export")
    @Log(title = "医嘱", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BieAdvice bieAdvice)
    {
        List<BieAdvice> list = bieAdviceService.selectBieAdviceList(bieAdvice);
        ExcelUtil<BieAdvice> util = new ExcelUtil<BieAdvice>(BieAdvice.class);
        return util.exportExcel(list, "医嘱数据");
    }

    /**
     * 新增医嘱
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存医嘱
     */
    @RequiresPermissions("system:advice:add")
    @Log(title = "医嘱", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BieAdvice bieAdvice)
    {

        Long userId = ShiroUtils.getUserId();
        //BiePatient biePatient = new BiePatient();
        //biePatient.setUserId(userId);
        BieDoctor bieDoctor=new BieDoctor();
        List<BieDoctor> bieDoctors = bieDoctorService.selectBieDoctorList(bieDoctor);
        bieAdvice.setDoctorId(bieDoctors.get(0).getDoctorId());
        //  System.out.println(userId);
//        List<BiePatient> biePatients = biePatientService.selectBiePatientList(biePatient);
//        String patientId = biePatients.get(0).getPatientId();

        return toAjax(bieAdviceService.insertBieAdvice(bieAdvice));
    }

    /**
     * 修改医嘱
     */
    @GetMapping("/edit/{adviceId}")
    public String edit(@PathVariable("adviceId") String adviceId, ModelMap mmap)
    {
        BieAdvice bieAdvice = bieAdviceService.selectBieAdviceById(adviceId);
        mmap.put("bieAdvice", bieAdvice);
        return prefix + "/edit";
    }

    /**
     * 修改保存医嘱
     */
    @RequiresPermissions("system:advice:edit")
    @Log(title = "医嘱", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BieAdvice bieAdvice)
    {
        return toAjax(bieAdviceService.updateBieAdvice(bieAdvice));
    }

    /**
     * 删除医嘱
     */
    @RequiresPermissions("system:advice:remove")
    @Log(title = "医嘱", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bieAdviceService.deleteBieAdviceByIds(ids));
    }
}
